import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/app/prisma/client';

export async function GET(req: NextRequest) {
  try {
    const { searchParams } = new URL(req.url);
    const query = searchParams.get('q');

    if (!query) {
      return NextResponse.json({ error: '请提供查询参数 "q"' }, { status: 400 });
    }

    const users = await prisma.user.findMany({
      where: {
        OR: [
          {
            username: {
              contains: query,
            },
          },
          {
            nickname: {
              contains: query,
            },
          },
        ],
      },
      select: {
        id: true,
        username: true,
        nickname: true,
        avatar: true,
        bio: true,
      },
      take: 20, // 限制最多返回20条结果
    });

    return NextResponse.json(users);
  } catch (error) {
    console.error('搜索用户失败:', error);
    return NextResponse.json({ error: '服务器错误' }, { status: 500 });
  }
} 